package com.globallogic.oopbasics.competence.generic;

import java.util.List;

import org.apache.log4j.Logger;

import com.globallogic.oopbasics.competence.container.TotalCompetence;
import com.globallogic.oopbasics.competence.entity.AbstractCompetency;
import com.globallogic.oopbasics.competence.entity.TechCompetency;
import com.globallogic.oopbasics.competence.exception.LevelException;

/**
 * Create CompetenceBox that contains one type of Competencies
 */
public class CompetenceBoxCreator {
	
	// Logger
	private Logger log = Logger.getLogger(LevelException.class);

	private CompetenceBox<AbstractCompetency, TechCompetency> box = new CompetenceBoxOneType<AbstractCompetency, TechCompetency>();

	public List<TechCompetency> getBoxContainer() {
		return box.getContainer();
	}

	public CompetenceBox<AbstractCompetency, TechCompetency> getBoxInstance() {
		return box;
	}

	private TotalCompetence totalCompetence;

	public TotalCompetence getTotalCompetence() {
		return totalCompetence;
	}

	public void setTotalCompetence(TotalCompetence totalCompetence) {
		this.totalCompetence = totalCompetence;
	}

	public CompetenceBoxCreator(TotalCompetence totalCompetence) {

		setTotalCompetence(totalCompetence);

		for (AbstractCompetency compValue : totalCompetence.getContainer()) {
			if (compValue instanceof TechCompetency) {
				try {
					box.put(new TechCompetency(compValue.getId(), compValue
							.getName(), compValue.getGlobalValue()));
				} catch (LevelException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	public boolean containsSame(
			CompetenceBox<AbstractCompetency, TechCompetency> boxAnother) {

		final CompetenceBox.EqualityComparator<AbstractCompetency> sameObject = new CompetenceBox.EqualityComparator<AbstractCompetency>() {

			@Override
			public boolean compare(List<?> first, List<?> second) {
				return first.equals(second);
			}
		};
		return box.containsSame(boxAnother, sameObject);
	}
}